/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package easybooking.clients.gui.report;

import easybooking.ReportClientController;
import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Tim Schröder
 * http://www.java2s.com/Code/Java/Swing-JFC/ExpandingorCollapsingAllNodesinaJTreeComponent.htm
 */
    public class RCMenuTree extends JTree {

    private static RCMenuTree ReportClientMenuTree = null;
    private static ReportClientController rcc;

    /**
     * Constructor of the RCMenuTree class, which passes the nodes to the super
     * class
     * @param root the root node of the tree
     */
    private RCMenuTree(TreeNode root) {
        super(root);
    }

    /**
     * Singelton implementation for the report client navigation tree, if a
     * ReportClientMenuTree exists, it will be returned, otherwise a new one
     * will be created
     * @param rc the current ReportClientMenuTree
     * @return ReportClientMenuTree
     */
    public static RCMenuTree ReportClientMenuTree(ReportClientController rc) {
        rcc = rc;
        if (ReportClientMenuTree == null) {
            RCMenuTree tree = new RCMenuTree(ReportClientMenuTree.createNodes());
            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.addTreeSelectionListener(rcc);
            return tree;
        }
        return ReportClientMenuTree;
    }

    /**
     * creates nodes of the tree
     * @return a TreeNode Object which sets all nodes for EBMenuTree Object
     */
    private static TreeNode createNodes() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("EasyBooking");
        {
            DefaultMutableTreeNode bookingNodes = new DefaultMutableTreeNode("Flight Seating");
            root.add(bookingNodes);

            DefaultMutableTreeNode flightNodes = new DefaultMutableTreeNode("Customer Report");
            root.add(flightNodes);
        }
        return root;
    }

    /**
     * expandes all nodes of the tree
     */
    public void expandAll() {
        TreeNode root = (TreeNode) this.getModel().getRoot();
        expandAll(this, new TreePath(root));
    }

    /**
     * expandes all nodes of the tree on a specific path
     * @param tree for the tree which should be expanded
     * @param parent for the parent node of the tree
     */
    private void expandAll(JTree tree, TreePath parent) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path);
            }
        }
        tree.expandPath(parent);
    }
}